// -*- coding: utf-8 -*- 
/**
 * Project: Java8InActionT
 * Creator: yanking
 * Create time: 2022-01-26 11:06
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.CustomerCollector;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.function.*;
import java.util.stream.Collector;

public class CustomerCollector implements Collector<Student, List<Student>, List<Student>> {
    // supply   the container that store the data process  accumulator
    @Override
    public Supplier<List<Student>> supplier() {
        return ArrayList::new;
    }

    // Core of the collector, collect the student data into specific List
    // here is to get the total scores of student
    @Override
    public BiConsumer<List<Student>, Student> accumulator() {
        return (l, e) -> {
            Predicate<Student> distinctStudent = student -> student.getId().equals(e.getId());
            boolean ifAdd = l.stream().noneMatch(distinctStudent);
            if (ifAdd) {
                // if there is not exits a student in list, set the student data to total, else add the course's score straightly
                // achieve the function of lazy realize
                e.setCourse(Student.Course.TOTAL);
                l.add(e);
            } else {
                l.stream().filter(distinctStudent).forEach(student -> student.setScore(student.getScore() + e.getScore()));
            }

        };
    }

    @Override
    public BinaryOperator<List<Student>> combiner() {
        return null;
    }

    @Override
    public Function<List<Student>, List<Student>> finisher() {
        return Function.identity();
    }

    @Override
    public Set<Characteristics> characteristics() {
        return EnumSet.of(Characteristics.IDENTITY_FINISH);
    }
    // supplier that provide the container to store the data process by acc

}
